<template>
  <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="员工工号" prop="id">
        <el-input v-model="searchForm.empId" placeholder="员工工号" :min="0" />
      </el-form-item>
      <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 18.75rem">
		  
		<el-date-picker
		  v-model="searchForm.time"
		  type="month"
		  placeholder="选择月"
				value-format="YYYY-MM"
				:disabledDate="check">
		</el-date-picker>
		
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
    <el-table
      :data="ScoreRuleList"
      border
      style="width: 95%; margin: 20px auto;border-radius: 8px; margin: 20px auto;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
      :header-cell-style="{
          'background': '#337ecc !important',
          'color': '#ffffff',
          'border': 'none !important'
        }">
	  <el-table-column prop="deptName" label="部门" width="" />
	  <el-table-column prop="position" label="岗位名称" width="" />
	  <el-table-column prop="name" label="被考核人" width="" />
	  <el-table-column prop="originalScore" label="总分" width=""/>
	  <el-table-column prop="updateScore" label="加减分" width="110"/>
	  <el-table-column prop="remark" label="修改说明" width="120"/>
    </el-table>
    <pagination
    class="pagination"
    v-show="ScoreListPage.total > 0"
    :total="ScoreListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="ScoreListPage.size"
    @pagination="handlePagination"
  />
</template>

<script setup>
import { ref, onBeforeMount, reactive, getCurrentInstance,watch } from "vue";
import {getEmpScoreTotalPastPage,reDelete,getEmpScoreTotalPastSearch} from "@/api/empScoreTotal"
import { ElMessage } from "element-plus";

const betweenTime=ref()
const searchForm = ref({
  empId: "",
  empName: "",
  page: 1,
  page_size: 10,
  time: "",
});


const ScoreListPage = ref([]);
const ScoreRuleList = ref([]);

watch(searchForm.value, (newValue, oldValue) => {
	searchEvent()
}, { deep: true });


onBeforeMount(() => {
  getEmpScoreTotalPastPage(searchForm.value).then((data) => {
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
});
watch(
  ScoreRuleList,
  (newVal) => {
    
  },
  { deep: true }
);

const searchEvent=()=>{
  // if(betweenTime.value!=""){
  //   searchForm.value.beginTime=betweenTime.value[0]
  //   searchForm.value.endTime=betweenTime.value[1]
  // }
  
  getEmpScoreTotalPastSearch(searchForm.value).then((data) => {
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
	
  });
}

const handlePagination=(value)=>{
  if(searchForm.value.empId==""&&searchForm.value.empName==""){
    getEmpScoreTotalPastPage(searchForm.value).then((data) => {
        ScoreListPage.value = data.data;
        ScoreRuleList.value = data.data.records;
    });
  }
  else{
	  
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getEmpScoreTotalPastSearch(searchForm.value).then(data=>{
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    })
  } 
}

const update = () => {
  searchForm.value.empId=""
  searchForm.value.empName=""
  
  getEmpScoreTotalPastPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
	  
    } else {
      ElMessage.error("更新失败");
    }
  });
};

const now = new Date();
const year = now.getFullYear();
// 获取当前月份（注意：月份从0开始，所以需要加1）
const month = now.getMonth() == 0? 12:now.getMonth();
// 格式化为 YYYY-MM 形式
const yearMonth = `${year}-${month < 10 ? '0' : ''}${month}`;

searchForm.value.time = yearMonth

const check = (time)=>{
	 return time.getTime() > new Date(new Date().toLocaleDateString()).getTime() - 31 * 24 * 3600 * 1000;
}

</script>
<style lang="scss" scoped>
.el-card {
  border-radius: 10px;
}
</style>
